package com.hmall.controller.back;

import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;

import com.hmall.mapper.*;

import com.hmall.model.*;

import com.hmall.service.PlanManageService;
import com.hdmall.business.common.util.HttpClientUtil;
import com.hmall.util.TokenUtil;
import com.hmall.vo.MallProductPlanItemVo;
import com.hmall.vo.PlanMonthVo;
import com.hmall.vo.PlanVo;

import com.hmall.vo.planMonthItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.math.BigDecimal;

import java.util.*;


@RestController
@RequestMapping("/manage/plan")
@Api(value = "生产计划管理操作API", description = "生产计划管理类API说明")
public class PlanManagerController {
    private final
    MallProductPlanMapper mallProductPlanMapper;

    private final
    MallProductPlanItemMapper mallProductPlanItemMapper;

    private final
    MallOrderMapper mallOrderMapper;

    private final
    MallDealerMapper mallDealerMapper;

    private final
    SysDeptMapper sysDeptMapper;

    private final
    SysCustomerMapper sysCustomerMapper;

    private final
    PlanManageService planManageService;

    private final
    MallProductPlanMonthMapper mallProductPlanMonthMapper;

    private final
    SysUserMapper sysUserMapper;

    private final
    MallProductPlanLogMapper mallProductPlanLogMapper;

    private final
    JedisPool jedisPool;

    private final
    SysUserDeptMapper sysUserDeptMapper;

    private TokenUtil tokenUtil = new TokenUtil();

    @Autowired
    public PlanManagerController(MallOrderMapper mallOrderMapper, MallProductPlanMapper mallProductPlanMapper, MallProductPlanItemMapper mallProductPlanItemMapper, MallDealerMapper mallDealerMapper, SysDeptMapper sysDeptMapper, SysCustomerMapper sysCustomerMapper, PlanManageService planManageService, MallProductPlanMonthMapper mallProductPlanMonthMapper, SysUserMapper sysUserMapper, MallProductPlanLogMapper mallProductPlanLogMapper, SysUserDeptMapper sysUserDeptMapper, JedisPool jedisPool) {
        this.mallOrderMapper = mallOrderMapper;
        this.mallProductPlanMapper = mallProductPlanMapper;
        this.mallProductPlanItemMapper = mallProductPlanItemMapper;
        this.mallDealerMapper = mallDealerMapper;
        this.sysDeptMapper = sysDeptMapper;
        this.sysCustomerMapper = sysCustomerMapper;
        this.planManageService = planManageService;
        this.mallProductPlanMonthMapper = mallProductPlanMonthMapper;
        this.sysUserMapper = sysUserMapper;
        this.mallProductPlanLogMapper = mallProductPlanLogMapper;
        this.sysUserDeptMapper = sysUserDeptMapper;
        this.jedisPool = jedisPool;
    }

    @AuthRequired
    @GetMapping("getSalesManPlanList")
    @ApiOperation(value = "获得业务员计划任务列表")
    public ServerResponse<List<PlanVo>> getSalesManPlanList(Integer user_id, Integer status) {
        List<PlanVo> planVoList = mallProductPlanMapper.selectSalesManPlanList(user_id, status);

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @AuthRequired
    @GetMapping("searchSalesManPlan")
    @ApiOperation(value = "搜索业务员计划任务列表")
    public ServerResponse<List<PlanVo>> searchSalesManPlan(Integer status, Integer user_id,
                                                           String plan_no) {
        List<PlanVo> planVoList = mallProductPlanMapper.selectSalesManPlanListBySearch(status,
                user_id, plan_no);

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @AuthRequired
    @GetMapping("getPlanList")
    @ApiOperation(value = "获得计划任务列表")
    public ServerResponse<List<PlanVo>> getPlanList(Integer status, Integer dept_id) {
        List<PlanVo> planVoList = mallProductPlanMapper.selectPlanList(status,
                dept_id);

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @AuthRequired
    @GetMapping("examPlan")
    @ApiOperation(value = "审核计划任务")
    public ServerResponse<String> examPlan(Integer plan_id, Integer status,
                                           String opinion) {
        MallProductPlan productPlan = mallProductPlanMapper.selectByPrimaryKey(plan_id);
        productPlan.setStatus(status);
        productPlan.setOpinion(opinion);
        mallProductPlanMapper.updateByPrimaryKey(productPlan);

        return ServerResponse.createBySuccess("审核成功");
    }

    @AuthRequired
    @GetMapping("searchPlan")
    @ApiOperation(value = "搜索计划任务列表")
    public ServerResponse<List<PlanVo>> searchPlan(Integer status, Integer dept_id,
                                                   String plan_no) {
        List<PlanVo> planVoList = mallProductPlanMapper.selectPlanListBySearch(status,
                dept_id, plan_no);

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @AuthRequired
    @GetMapping("getMonthData")
    @ApiOperation(value = "获得月份数据")
    public ServerResponse<List<MallProductPlan>> getMonthData(Integer year, Integer month,
                                                              Integer dept_id) {
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        final Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);

        final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        List<MallOrder> mallOrderList = mallOrderMapper.selectByMonth(dealer_id,
                startTime, endTime);
        List<MallProductPlan> mallProductPlanList = mallProductPlanMapper.selectByMonth(dealer_id,
                startTime, endTime);
        List<SysDept> sysDeptList = sysDeptMapper.selectByMonth(dealer_id,
                startTime, endTime);

        return ServerResponse.createBySuccess(mallProductPlanList);
    }

    @AuthRequired
    @GetMapping("searchMyPlan")
    @ApiOperation(value = "搜索业务员计划任务列表")
    public ServerResponse<List<PlanVo>> searchMyPlan(Integer year, Integer month, Integer salesman_id,
                                                     Integer customer_id, Integer plan_id) {
        Date startTime = null;
        Date endTime = null;
        if (plan_id == null) {
            if (year != null && month != null) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month - 1);

                final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, start);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                startTime = cal.getTime();
                final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, end);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                endTime = cal.getTime();
            } else {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.MONTH, 0);

                final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, start);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                startTime = cal.getTime();
                cal.set(Calendar.MONTH, 11);
                final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, end);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                endTime = cal.getTime();
            }
        }

        List<PlanVo> planVoList = mallProductPlanMapper.selectMyCustomerPlanList(salesman_id,
                startTime, endTime, customer_id, plan_id);

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @GetMapping("searchDealerPlan")
    @ApiOperation(value = "搜索办事处计划任务列表")
    public ServerResponse searchDealerPlan(Integer year, Integer month, Integer dept_id,
                                           Integer customer_id, Integer plan_id, String token) throws Exception {

        if (token != null && !token.equals("")) {
            SysUser user;
            Jedis jedis = jedisPool.getResource();
            user = tokenUtil.tokenToUser(jedis, token).getData();
            jedis.close();
            if (user == null) {
                return ServerResponse.createByErrorMessage("token错误");
            }
            SysUserDept sysUserDept = sysUserDeptMapper.selectByUserId(user.getUserId());
            if (sysUserDept == null) {
                return ServerResponse.createByErrorMessage("该用户没有绑定公司");
            }
            customer_id = sysCustomerMapper.selectCustomerId(user.getUserId());
            if (customer_id == null) {
                return ServerResponse.createByErrorMessage("该用户资质审核未通过");
            }
        }
        Date startTime = null;
        Date endTime = null;
        if (plan_id == null) {
            if (year != null && month != null) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month - 1);

                final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, start);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                startTime = cal.getTime();
                final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, end);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                endTime = cal.getTime();
            } else if (year != null) {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, 0);

                final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, start);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                startTime = cal.getTime();
                cal.set(Calendar.MONTH, 11);
                final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, end);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                endTime = cal.getTime();
            } else {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.MONTH, 0);

                final int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, start);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);

                startTime = cal.getTime();
                cal.set(Calendar.MONTH, 11);
                final int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
                cal.set(Calendar.DAY_OF_MONTH, end);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 999);
                endTime = cal.getTime();
            }
        }


        List<PlanVo> planVoList = mallProductPlanMapper.selectDealerCustomerPlanList(startTime, endTime, customer_id, plan_id);


        Calendar calendar = Calendar.getInstance();
        final int start = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, start);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        startTime = calendar.getTime();
        final int end = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, end);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        endTime = calendar.getTime();

        for (PlanVo aPlanVoList : planVoList) {
            aPlanVoList.setNewData(new Date());
        }

        for (int i = 0; i < planVoList.size(); i++) {
            Date time = mallProductPlanMapper.selectByPrimaryKey(planVoList.get(i).getPlan_id()).getCreateTime();
            System.out.println(startTime.toLocaleString());
            System.out.println(endTime.toLocaleString());
            System.out.println(time.toLocaleString());
            System.out.println(startTime.before(time));
            System.out.println(endTime.after(time));
            System.out.println(time.after(startTime));
            System.out.println(time.after(endTime));
            if ((startTime.before(time) && endTime.after(time)) || startTime.equals(time)) {
                PlanVo planVo = planVoList.get(i);
                planVoList.remove(i);
                planVoList.add(0, planVo);
                break;
            }
        }

        for (PlanVo aPlanVoList : planVoList) {
            System.out.println(aPlanVoList.toString());
            MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(aPlanVoList.getPlan_id());
            List<MallProductPlanItemVo> mallProductPlanItems = mallProductPlanItemMapper.selectByPlanId(mallProductPlan.getPlanNo());
            aPlanVoList.setProductList(mallProductPlanItems);
        }

        System.out.println(planVoList);

        return ServerResponse.createBySuccess(planVoList);
    }

    @AuthRequired
    @PostMapping("editPlanItem")
    @ApiOperation(value = "编辑计划产品")
    public ServerResponse editPlanItem(Integer plan_item_id, BigDecimal amount) {
        MallProductPlanItem mallProductPlanItem = mallProductPlanItemMapper.selectByPrimaryKey(plan_item_id);
        if (mallProductPlanItem == null) {
            return ServerResponse.createByErrorMessage("修改失败");
        }
        mallProductPlanItem.setAmount(amount);
        mallProductPlanItemMapper.updateByPrimaryKey(mallProductPlanItem);
        return ServerResponse.createBySuccess("编辑成功");
    }

    @AuthRequired
    @DeleteMapping("deletePlanItem")
    @ApiOperation(value = "删除计划产品")
    public ServerResponse<String> deletePlanItem(Integer plan_item_id) {
        MallProductPlan mallProductPlan = mallProductPlanMapper.selectByNo(mallProductPlanItemMapper.selectByPrimaryKey(plan_item_id).getPlanNo());
        mallProductPlanItemMapper.deleteByPrimaryKey(plan_item_id);

        Map<String, String> map = new HashMap<>();
        // map.put("address", shippingId.toString());
        map.put("id", mallProductPlan.getPlanId().toString());
        map.put("deal_way", mallProductPlan.getDealWay());
        map.put("remark", mallProductPlan.getRemark());

        map.put("create_time", mallProductPlan.getCreateTime().toString());
        String statusStr = "";
        switch (mallProductPlan.getStatus()) {
            case 0: {
                statusStr = "未审核";
                break;
            }
            case 1: {
                statusStr = "审核中";
                break;
            }
            case 2: {
                statusStr = "通过";
                break;
            }
            case 3: {
                statusStr = "未通过";
                break;
            }
        }
        map.put("status", statusStr);
        String salesman_name = sysUserMapper.selectByPrimaryKey(mallProductPlan.getSaleUserId()).getUsername();
        map.put("salesman_name", salesman_name);
        map.put("plan_no", mallProductPlan.getPlanNo().toString());
        map.put("opinion", mallProductPlan.getOpinion());

        String username = sysUserMapper.selectByPrimaryKey(mallProductPlan.getUserId()).getUsername();
        map.put("username", username);
        map.put("productList", "");
        String s = HttpClientUtil.doPostWithParam("http://47.98.171.71:8081/hdfind/updatePlan", map);

        return ServerResponse.createBySuccess("删除成功");
    }

    @AuthRequired
    @PostMapping("addPlanProduct")
    @ApiOperation(value = "添加计划产品")
    public ServerResponse<String> addPlanProduct(Integer plan_id, Integer product_id, String grade, BigDecimal amount) {
        System.out.println(amount);
        MallProductPlan mallProductPlan = mallProductPlanMapper.selectByPrimaryKey(plan_id);
        MallProductPlanItem mallProductPlanItem2 = mallProductPlanItemMapper.checkItem(mallProductPlan.getPlanNo(), product_id, grade);
        if (mallProductPlanItem2 == null) {
            MallProductPlanItem mallProductPlanItem = new MallProductPlanItem();
            mallProductPlanItem.setAmount(amount);
            mallProductPlanItem.setGrade(grade);
            mallProductPlanItem.setPlanNo(mallProductPlan.getPlanNo());
            mallProductPlanItem.setProductId(product_id);
            mallProductPlanItemMapper.insert(mallProductPlanItem);
        } else {
            mallProductPlanItem2.setAmount(mallProductPlanItem2.getAmount().add(amount));
            mallProductPlanItemMapper.insert(mallProductPlanItem2);
        }

        Map<String, String> map = new HashMap<>();
        // map.put("address", shippingId.toString());
        map.put("id", mallProductPlan.getPlanId().toString());
        map.put("deal_way", mallProductPlan.getDealWay());
        map.put("remark", mallProductPlan.getRemark());

        map.put("create_time", mallProductPlan.getCreateTime().toString());
        String statusStr = "";
        switch (mallProductPlan.getStatus()) {
            case 0: {
                statusStr = "未审核";
                break;
            }
            case 1: {
                statusStr = "审核中";
                break;
            }
            case 2: {
                statusStr = "通过";
                break;
            }
            case 3: {
                statusStr = "未通过";
                break;
            }
        }
        map.put("status", statusStr);
        String salesman_name = sysUserMapper.selectByPrimaryKey(mallProductPlan.getSaleUserId()).getUsername();
        map.put("salesman_name", salesman_name);
        map.put("plan_no", mallProductPlan.getPlanNo().toString());
        map.put("opinion", mallProductPlan.getOpinion());

        String username = sysUserMapper.selectByPrimaryKey(mallProductPlan.getUserId()).getUsername();
        map.put("username", username);
        map.put("productList", "");
        String s = HttpClientUtil.doPostWithParam("http://47.98.171.71:8081/hdfind/updatePlan", map);

        return ServerResponse.createBySuccess("添加成功");
    }

    @AuthRequired
    @PostMapping("createCustomerPlan")
    @ApiOperation(value = "创建客户计划")
    public ServerResponse<String> createCustomerPlan() {
        List<SysCustomer> list = sysCustomerMapper.selectAll();
        for (SysCustomer aList : list) {
            Integer customer_id = aList.getCustomerId();
            Integer user_id = sysCustomerMapper.selectCustomerUserId(customer_id);
            SysCustomer sysCustomer = sysCustomerMapper.selectByPrimaryKey(customer_id);
            Integer sale_user_id = sysCustomer.getSalesmanId();
            if (user_id == null || sale_user_id == null) {
                System.out.println("该客户下没有用户");
            } else {
                List<PlanVo> list2 = mallProductPlanMapper.selectSalesManPlanList(user_id, null);
                if (list2.size() == 0) {
                    //创建今年十二个月的生成计划
                    Calendar calendar = Calendar.getInstance();
                    calendar.set(Calendar.DAY_OF_MONTH, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    for (int j = 1; j <= 12; j++) {
                        calendar.set(Calendar.MONTH, j - 1);
                        planManageService.createPlan(customer_id, calendar.getTime());
                    }
                } else {
                    System.out.println("已存在计划");
                }
            }
        }
        return ServerResponse.createBySuccess("创建成功");
    }

    @AuthRequired
    @PostMapping("createPlanMonth")
    @ApiOperation(value = "创建办事处每月计划汇总表")
    public ServerResponse<String> createPlanMonth() {
        List<MallDealer> dealerList = mallDealerMapper.selectAll();
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        int year = calendar.get(Calendar.YEAR);
        for (MallDealer aDealerList : dealerList) {
            MallProductPlanMonth mallProductPlanMonth = new MallProductPlanMonth();
            mallProductPlanMonth.setCreateTime(new Date());
            mallProductPlanMonth.setDealerId(aDealerList.getDealerId());
            mallProductPlanMonth.setMonth(month);
            mallProductPlanMonth.setYear(year);
            mallProductPlanMonth.setStatus(0);
            mallProductPlanMonthMapper.insert(mallProductPlanMonth);
        }
        return ServerResponse.createBySuccess("创建成功");
    }

    @GetMapping("getMonthPlan")
    @ApiOperation(value = "获得每月计划汇总表")
    public ServerResponse<List<Object>> getMonthPlan(Integer dept_id, Integer year, Integer month, String token) throws Exception {
        if (token != null && !token.equals("")) {
            SysUser user;
            Jedis jedis = jedisPool.getResource();
            user = tokenUtil.tokenToUser(jedis, token).getData();
            jedis.close();
            dept_id = user.getDeptId();
        }
        Integer dealer_id = mallDealerMapper.selectDealerId(dept_id);
        MallProductPlanMonth mallProductPlanMonth = mallProductPlanMonthMapper.selectMonthPlan(dealer_id, year, month);
        Calendar cal = Calendar.getInstance();
        if (year != null && month != null) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, month - 1);
        }
        int start = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, start);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        Date startTime = cal.getTime();
        int end = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, end);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);

        Date endTime = cal.getTime();
        List<PlanMonthVo> planMonthVoList = mallProductPlanMapper.selectPlanMonthList(dealer_id, startTime, endTime);

        for (PlanMonthVo aPlanMonthVoList : planMonthVoList) {
            List<planMonthItemVo> planMonthVoList2 = mallProductPlanItemMapper.selectMonthItem(aPlanMonthVoList.getProduct_id(), aPlanMonthVoList.getGrade(), startTime, endTime);
            aPlanMonthVoList.setItemList(planMonthVoList2);
        }
        List<MallProductPlanLog> mallProductPlanLog = new ArrayList<>();
        if (mallProductPlanMonth != null) {
            mallProductPlanLog = mallProductPlanLogMapper.selectById(mallProductPlanMonth.getMallProductPlanMonthId());
        }

        List<Object> list = new ArrayList<>();
        list.add(mallProductPlanLog);
        list.add(mallProductPlanMonth);
        list.add(planMonthVoList);
        return ServerResponse.createBySuccess(list);
    }

    @AuthRequired
    @PostMapping("updatePlanItem")
    @ApiOperation(value = "修改计划")
    public ServerResponse<String> updatePlanItem(Integer mall_product_plan_month_id, Integer user_id, Integer plan_item_id, BigDecimal amount) {
        String roleName = sysUserMapper.selectRoleName(user_id);
        MallProductPlanItem mallProductPlanItem = mallProductPlanItemMapper.selectByPrimaryKey(plan_item_id);
        mallProductPlanItem.setAmount(amount);
        mallProductPlanItem.setResidualQuantity(amount);
        mallProductPlanItemMapper.updateByPrimaryKey(mallProductPlanItem);
        MallProductPlanLog mallProductPlanLog = new MallProductPlanLog();
        String content = roleName + "在" + (new Date()).toLocaleString() + "将编号为" + mallProductPlanItem.getPlanNo() + "的计划单下的编号为" + mallProductPlanItem.getProductId() + "的产品数量修改为" + amount;
        mallProductPlanLog.setPlanItemId(plan_item_id);
        mallProductPlanLog.setContent(content);
        mallProductPlanLog.setMallProductPlanMonthId(mall_product_plan_month_id);
        mallProductPlanLogMapper.insert(mallProductPlanLog);
        return ServerResponse.createBySuccess("修改成功");
    }


    @GetMapping("test")
    @ApiOperation(value = "aaa")
    public void test() {

    }

}
